# coding=utf-8
import time

import allure
from common.assert_operator import *
from common.db import *
from common.read_yaml import *
from common.utils import Utils

string_assert = string_assert() # 字符串校验类
pact = Pact() # 契约校验类
execute_sql = sql() # sql集合
utils = Utils()
data = ReadYaml_By_Root_Dict("case/99_outside_api_test/01_workflow_outside_test/99_01_1_Token_IsCanCancelProcess_CancelProcess.yml").get_yaml_data()  # 读取数据

@allure.feature('测试工作流外部接口：获取token、是否可作废流程、作废流程')  # 测试报告显示测试功能
class Test_99_01_1_Token_IsCanCancelProcess_CancelProcess():
    """
    测试接口：
    /wf/api/Common/Token
    /wf/api/ProcessEntiy/IsCanCancelProcess
    /wf/api/ProcessEntiy/CancelProcess
    """
    # 获取yml文件中Common_Token的数据参数
    Common_Token = data['Common_Token']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Common_Token, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Common_Token, ids=ids_list)
    def test_get_wf_token(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        user_guid = param['body']    #获取入参
        print(user_guid)
        resp = user_login.goto_Outside_Api().get_wf_token(user_guid)    # 执行请求
        dict_assert(expect,resp.json())    # 结果断言

    # 获取yml文件中IsCanCancelProcess的数据参数
    Params = data['IsCanCancelProcess']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_IsCanCancelProcess(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['description']}")  # # 动态解析用例描述在测试报告中展示
        business_guid = param['body']    #获取入参
        user_guid = utils.get_user_guid_by_code(param['user'])
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        resp = main.IsCanCancelProcess(business_guid,user_guid)    # 执行判断是否能够业务系统作废请求
        dict_assert(expect,resp.json())    # 结果断言

    # 获取yml文件中CancelProcess的数据参数
    Params = data['CancelProcess']
    # 获取yml的title，并作为ids的值传给用例
    ids_list = jsonpath.jsonpath(Params, "$[*][0]")
    @pytest.mark.parametrize("title, param, expect", Params, ids=ids_list)
    def test_CancelProcess(self, user_login, title, param, expect):
        allure.dynamic.description(f"{param['description']}")   ## 动态解析用例描述在测试报告中展示
        business_guid = param['body']    #获取入参
        user_guid = utils.get_user_guid_by_code(param['user'])  #获取用户id
        main = user_login.goto_Outside_Api()    # 进入外部接口类
        resp = main.CancelProcess(business_guid,user_guid)    # 执行判断是否能够业务系统作废请求
        dict_assert(expect['response'],resp.json())    # 请求结果断言
        try:
            # yml中不存在proc_id，则不进行断言
            proc_id = param['proc_id']
            # 数据库查询结果后，进行断言
            sql = execute_sql.query_node_sql_by(proc_id=proc_id,history=True)
            result = utils.query_until_all_matched(sql,expect['db_check'],query_alias='db_check')
            list_each_equal(expect['db_check'], result)
        except KeyError:
            pass



